import { _decorator, Component, instantiate, Label, Node, ScrollView, Sprite, Toggle, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, shopData } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { shopItem } from '../item/shopItem';
import { Util } from '../Util';
import { chatItem } from '../item/chatItem';
const { ccclass, property } = _decorator;

@ccclass('game')
export class game extends Component {

    @property(Node)  
    otherNode: Node = null;

    @property(Node)  
    popupRank: Node = null;

    @property(Node)  
    popupInvite: Node = null;

    @property(Node)  
    popupTask: Node = null;

    @property(Node)  
    popupMy: Node = null;

    @property(Sprite)  
    avatar: Sprite = null;

    @property(Label)  
    nameLab: Label = null; 

    @property(Label)  
    IDLab: Label = null; 

    @property(Label)  
    coinLab: Label = null;
    
    @property(Label)  
    dayLab: Label = null; 

    @property(Label)  
    roleNumLab: Label = null; 

    @property(ScrollView)  
    shopScroll: ScrollView = null; 

    @property(ScrollView)  
    logScroll: ScrollView = null;
    
    @property(Node)  
    toggroup: Node = null;

    protected start(): void {

        EventManager.eventTarget.on(EventManager.REFRESH_GAME, this.refresh, this);

        EventManager.eventTarget.on(EventManager.REFRESH_LOG, this.refreshLog, this);

        EventManager.eventTarget.on(EventManager.GAME_CHANGE_VIEW, this.onChangeView, this);

        this.hideOtherNode()
    }

    onEnable() {
        this.refresh();

        this.refreshLog();
    }

    onChangeView(index:number){
        let str;
        if (index==0){
            str = "rank";
        }else if (index == 1){
            str = "invite";
        }
        else if (index == 2){
            str = "main";
        }
        else if (index == 3){
            str = "task";
        }
        else if (index == 4){
            str = "my";
        }
        for (let ind = 0; ind < this.toggroup.children.length; ind++) {
            const element = this.toggroup.children[ind];
            if (ind == index){
                element.getComponent(Toggle).isChecked = true;
            }
            
        }
        this.onClickHandler(null, str)
    }

    refresh(){

        Util.loadRemoteUrl(GameData.getIns().userData.avatar, this.avatar);

        this.nameLab.string = GameData.getIns().userData.nickname;

        this.IDLab.string = "ID: " + GameData.getIns().userData.id;

        this.coinLab.string = GameData.getIns().userData.coin + "";

        // 刷新商店
        for (let index = 0; index < this.shopScroll.content.children.length; index++) {
            const element = this.shopScroll.content.children[index];
            element.active = false;
        }

        for (let index = 0; index < GameData.shopDataList.length + 1; index++) {
            let itemData:shopData = GameData.shopDataList[index];
            let item = this.shopScroll.content.children[index];
            if (!item){
                item = instantiate(this.shopScroll.content.children[0])
                item.parent = this.shopScroll.content;
            }
            item.active = true;
            item.getComponent(shopItem).refresh(itemData);
        }
    }

    /**刷新操作日志 */
    refreshLog(){
        // 刷新商店
        for (let index = 0; index < this.logScroll.content.children.length; index++) {
            const element = this.logScroll.content.children[index];
            element.active = false;
        }

        for (let index = 0; index < GameData.logList.length; index++) {
            let itemData:shopData = GameData.logList[index];
            let item = this.logScroll.content.children[index];
            if (!item){
                item = instantiate(this.logScroll.content.children[0])
                item.parent = this.logScroll.content;
            }
            item.active = true;
            // item.getComponent(shopItem).refresh(itemData);
        }
    }

    onClickHandler(event:Event, str:string){
        if (str == "ruleBtn"){
            UIManager.open(UIManager.uiNamePath.popupRule);
        }
        else if (str == "shopBtn"){

        }
        else if (str == "rank"){
            this.hideOtherNode()
            this.popupRank.active = true;
        }
        else if (str == "invite"){
            this.hideOtherNode()
            this.popupInvite.active = true;
        }
        else if (str == "task"){
            this.hideOtherNode()
            this.popupTask.active = true;
        }
        else if (str == "my"){
            this.hideOtherNode()
            this.popupMy.active = true;
        }
        else if (str == "main"){
            this.hideOtherNode()
        }
    }

    hideOtherNode(){
        for (let index = 0; index < this.otherNode.children.length; index++) {
            const element = this.otherNode.children[index];
            element.active = false;
        }
    }

  
}

